/* This file is part of the Pardus Economy Planner.

   The Pardus Economy Planner is free software: you can redistribute it
   and/or modify it under the terms of the GNU General Public License
   as published by the Free Software Foundation, either version 3 of
   the License, or (at your option) any later version.

   The Pardus Economy Planner is distributed in the hope that it
   will be useful, but WITHOUT ANY WARRANTY; without even the implied
   warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
   See the GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with the Pardus Economy Planner.  If not, see
   <http://www.gnu.org/licenses/>.
*/

/// <reference path="../base.js" />
/// <reference path="../Libs/Mootools-Core.js" />

/* Pardus Sector Tool
 * (C) Yassen 2010
 *
 * config.js
 * Defines functions for interacting with the configuration options.
 *
 * Functions:
 * void    Read()
 * void    Write()
 * String  Get(key)
 * void    Set(key, value)
 *
 */

Utility.Config.Data = {
  "Version": new ConfigValue("Version", CookieVersion, "Version", "Cookie Version"),
  "Imagepack": new ConfigValue("Imagepack", "http://static.pardus.at/img/stdhq", "Image Pack", ""),
  "DefaultBuildingLevel": new ConfigValue("DefaultBuildingLevel", 1, "Default Building Level", "")
};

Utility.Config.Ready = false;
Utility.Config.CookieName = "PardusEconConfig";

Utility.Config.Read = function()
{
  if (Utility.Config.Ready)
    return;

  // Read Cookie (JSON format)
  var cookie = Cookie.read(Utility.Config.CookieName);

  if (cookie == null || JSON.decode(cookie, true) == null)
  {
    Utility.Config.Write();
    cookie = Cookie.read(Utility.Config.CookieName);
  }

  cookie = JSON.decode(cookie);

  for (var key in cookie)
  {
    if (!cookie.hasOwnProperty(key))
      continue;

    Utility.Config.Data[key].value = cookie[key];
  }
  
  Utility.Config.Ready = true;

  EventSink.fireEvent("ConfigLoaded");
}

Utility.Config.Write = function()
{
  var condensed = {};

  for (var key in Utility.Config.Data)
  {
    if (!Utility.Config.Data.hasOwnProperty(key))
      continue;

    condensed[key] = Utility.Config.Data[key].value;
  }
  
  Cookie.write(Utility.Config.CookieName, JSON.encode(condensed), { duration: 365 });
}

Utility.Config.Get = function(key)
{
  /// <summary>Gets the value associated with the key.</summary>
  /// <param name="key" type="String">Key to find value with.</param>
  /// <returns type="String">Value</returns>

  Utility.Config.Read();
  return Utility.Config.Data[key].value;
}

Utility.Config.Set = function(key, value)
{
  /// <summary>Sets key to value</summary>
  /// <param name="key" type="String">Key to set the value of.</param>
  /// <param name="value" type="Mixed">Value to set key to.</param>
  /// <returns type="void"></returns>

  if (Utility.Config.Data[key].value == value) // No change
    return;
    
  Utility.Config.Data[key].value = value;
  Utility.Config.Write();
}